package com.wzdigit.wms.raw.service.trsn.report;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.trsn.TrsnRequest;

import com.wzdigit.wms.basic.enums.GdnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.GdnTypeEnum;
import com.wzdigit.wms.basic.util.enums.SystemModuleEnum;
import com.wzdigit.wms.common.entity.ExcelExportConfig;
import com.wzdigit.wms.common.utils.HttpExcel;
import com.wzdigit.wms.raw.client.request.trsn.report.gdntrsn.GdnTrsnRequest;
import com.wzdigit.wms.raw.client.response.gdn.ortherGdn.OrtherGdnEnums;
import com.wzdigit.wms.raw.client.response.trsn.report.gdntrsn.GdnTrsnResponse;
import com.wzdigit.wms.raw.mapper.trsn.report.GdnTrsnMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出库凭证Service
 * @Auther: cbwei
 * @create: 2021/10/18 16:39
 */
@Service
@Slf4j
public class GdnTrsnService {

    @Autowired
    private GdnTrsnMapper gdnTrsnMapper;

    //分页查询入库凭证报表
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public PagerInfo<List<GdnTrsnResponse>> searchGdnTrsnReport(
            GdnTrsnRequest search, Pager<?> pager){
        List<String> gdnTypeList = GdnTypeEnum.getEnumBySystemModule(SystemModuleEnum.RAW);
        Page<GdnTrsnResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        List<GdnTrsnResponse> datas = this.selectGdnTrsn(search,gdnTypeList);
        PagerInfo<List<GdnTrsnResponse>> pagerInfo = new PagerInfo<>();
        pagerInfo.setTotalCount(page.getTotal());
        pagerInfo.setItems(datas);
        return pagerInfo;
    }

    //导出出库凭证报表
    @Transactional
    public void exportGdnTrsnReport(
            GdnTrsnRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        ServletOutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            List<String> gdnTypeList = GdnTypeEnum.getEnumBySystemModule(SystemModuleEnum.RAW);
            List<GdnTrsnResponse> resultDatas = this.selectGdnTrsn(search,gdnTypeList);
            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("出库凭证", "noteNo", 120));
            configs.add(new ExcelExportConfig("来源单据", "fmDoc", 120));
            configs.add(new ExcelExportConfig("单据类型", "noteTypeString", 120));
            configs.add(new ExcelExportConfig("物料编码", "skuCode", 120));
            configs.add(new ExcelExportConfig("物料描述", "descCn", 120));
            configs.add(new ExcelExportConfig("供应商", "vendorName", 120));
            configs.add(new ExcelExportConfig("库位", "locDesc", 120));
            configs.add(new ExcelExportConfig("状态", "statusString", 120));
            configs.add(new ExcelExportConfig("发货数量", "shipQty", 120));
            configs.add(new ExcelExportConfig("发货人", "createUser", 120));
            configs.add(new ExcelExportConfig("发货时间", "shipTime", 120));
            workbook = HttpExcel.newListToExcel(resultDatas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "出库凭证报表";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if(workbook != null) {
                workbook.close();
            }
        }

    }

    //查询出库凭证报表【复用】
    private List<GdnTrsnResponse> selectGdnTrsn(GdnTrsnRequest search,List<String> gdnTypeList){
        //设置查询信息
        if (search.getBeginTime()!=null){
            search.setBeginTimeDate(new Date(search.getBeginTime()));
        }
        if (search.getEndTime()!=null){
            search.setEndTimeDate(new Date(search.getEndTime()));
        }
        if (search.getFilter()!=null){
            search.setFilter(search.getFilter().replace(" ",""));
        }
        if (search.getNoteNo()!=null){
            search.setNoteNo(search.getNoteNo().replace(" ",""));
        }
        if (search.getFmDoc()!=null){
            search.setFmDoc(search.getFmDoc().replace(" ",""));
        }
        List<GdnTrsnResponse> datas = gdnTrsnMapper.selectAllGdnTrsn(search,gdnTypeList);
        //设置返回信息
        for (GdnTrsnResponse data : datas) {
            if (data.getStatus()!=null){
                GdnDtlStatusEnum dtlStatus = GdnDtlStatusEnum.getEnumByCode(data.getStatus());
                if (dtlStatus!=null){
                    data.setStatusString(dtlStatus.enumName);
                }
            }
            if (data.getNoteType()!=null){
                GdnTypeEnum gdnTypeEnum = GdnTypeEnum.getEnumByCode(data.getNoteType());
                if (gdnTypeEnum!=null){
                    data.setNoteTypeString(gdnTypeEnum.enumName);
                }
            }
        }
        return datas;
    }

    //查询出库凭证单据类型下拉框
    public List<OrtherGdnEnums> getOrtherGdnEnumsList(){
        List<String> gdnTypeList = GdnTypeEnum.getEnumBySystemModule(SystemModuleEnum.RAW);
        List<OrtherGdnEnums> datas=new ArrayList<>();
        for (String gdnTypeString : gdnTypeList) {
            GdnTypeEnum gdnTypeEnum = GdnTypeEnum.getEnumByCode(gdnTypeString);
            if (gdnTypeEnum!=null){
                OrtherGdnEnums data=new OrtherGdnEnums();
                data.setCode(gdnTypeString);
                data.setEnumName(gdnTypeEnum.getEnumName());
                datas.add(data);
            }
        }
        return datas;
    }

}
